﻿
#include "slidingSideWindow.h"
#include <QDebug>
SlidingSideWindow::SlidingSideWindow(QWidget *parent)
    : QFrame(parent)
{
    initUI();
    configInit();
}

SlidingSideWindow::~SlidingSideWindow()
{
}

void SlidingSideWindow::enterEvent(QEvent *event)
{
    QFrame::enterEvent(event);
    // 跳过第一次
    if (!enterFlag)
    {
        enterFlag = true;
        anewRecordPos();            //重新记录位置
        return;
    }

    // 显示包含文字的按钮
    QList<customBtn *> frames = this->findChildren<customBtn *>();
    for (auto item : frames)
    {
        item->pushButton()->show();
    }

    animationGroup->setDirection(QAbstractAnimation::Backward);
    animationGroup->start();
}

void SlidingSideWindow::leaveEvent(QEvent *event)
{
    QFrame::leaveEvent(event);
    QList<customBtn *> frames = this->findChildren<customBtn *>();
    for (auto item : frames)
    {
        item->pushButton()->hide();
    }

    animationGroup->setDirection(QAbstractAnimation::Forward);
    animationGroup->start();
}


void SlidingSideWindow::initUI()
{
    this->resize(141, 271);
    this->setStyleSheet("QFrame{border-radius: 15px;"
            "background-color: rgba(255, 116, 0,200);}");
    // 判断是否第一次进入
    enterFlag = false;

    //并行播放多个动画的动画组
    animationGroup = new QParallelAnimationGroup(this);
}

void SlidingSideWindow::configInit()
{
    auto childFrame =new customBtn(this,"Close",":/icon/Close.png");
    childFrame->setGeometry(QRect(10, 20, 110, 30));
    auto childFrame_2 =new customBtn(this,"Enter",":/icon/Enter.png");
    childFrame_2->setGeometry(QRect(10, 60, 110, 30));
    auto childFrame_3 =new customBtn(this,"SetKey",":/icon/SetKey.png");
    childFrame_3->setGeometry(QRect(10, 100, 110, 30));
    auto childFrame_4 =new customBtn(this,"Resign",":/icon/Resign.png");
    childFrame_4->setGeometry(QRect(10, 140, 110, 30));
    auto childFrame_5 =new customBtn(this,"Verify",":/icon/Verify.png");
    childFrame_5->setGeometry(QRect(10, 180, 110, 30));
    auto childFrame_6 =new customBtn(this,"Share",":/icon/Share.png");
    childFrame_6->setGeometry(QRect(10, 220, 110, 30));
    m_customBtn_widthShow = childFrame->getWidth();
    m_customBtn_widthHide = 40;
    m_customBtn_height = childFrame->getHeight();

    anewRecordPos();            //重新记录位置
}

void SlidingSideWindow::animationGroupConfig()
{
    // 清空动画组
    animationGroup->clear();

    /*在leaveEvent和enterEvent中对右侧按钮隐藏后，通过QPropertyAnimation属性动画修改customBtn的
     *自定义属性m_size，以更改customBtn底下的QFrame大小。否则QFrame存在background-color就看不到
     *customBtn图标的右侧圆角*/
    // 按钮动画
    QList<customBtn *> frames = this->findChildren<customBtn *>();
    for (auto childFrame : frames)
    {
        QPropertyAnimation *btnAnimation = new QPropertyAnimation(childFrame, "m_size");
        btnAnimation->setDuration(150);
        btnAnimation->setStartValue(QSize(m_customBtn_widthShow, m_customBtn_height));
        btnAnimation->setEndValue(QSize(m_customBtn_widthHide, m_customBtn_height));
        animationGroup->addAnimation(btnAnimation);      // 添加属性动画到动画组
    }

    // 侧边栏动画
    QPropertyAnimation *frameAnimation = new QPropertyAnimation(this, "geometry");
    frameAnimation->setDuration(150);
    frameAnimation->setStartValue(QRect(m_xShow, m_y, m_widthShow, this->height()));
    frameAnimation->setEndValue(QRect(m_xHide, m_y, m_widthHide, this->height()));
    animationGroup->addAnimation(frameAnimation);         // 添加属性动画到动画组
}

//重新计算位置
void SlidingSideWindow::anewRecordPos()
{
    // 设置变形部件尺寸
    m_widthHide = 61;
    m_widthShow = this->width();
    m_widthChange = m_widthShow - m_widthHide;
    m_xShow = this->x();
    m_xHide = this->x() + m_widthChange / 2;
    m_y = this->y();

    animationGroupConfig();
}


